package com.huawei.hianalytics.log.f;

import android.text.TextUtils;
import com.huawei.hianalytics.log.e.d;
import com.huawei.hianalytics.log.f.a.a;
import com.huawei.hianalytics.log.f.a.b;
import com.huawei.hianalytics.log.f.a.c;
import com.huawei.hms.aaid.constant.AaidIdConstant;
import com.huawei.hms.framework.common.ContainerUtils;
import com.huawei.hms.framework.network.restclient.hwhttp.HttpContants;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel.MapMode;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import org.json.JSONArray;
import org.json.JSONObject;

class f implements e {
    f() {
    }

    public b a(String str, String str2) {
        b bVar = new b();
        File[] listFiles = new File(str).listFiles();
        if (listFiles == null || listFiles.length < 1) {
            com.huawei.hianalytics.g.b.d("SendManagerImpl", "not has send file");
            return null;
        }
        File file = listFiles[0];
        if (file.length() > 5242880) {
            com.huawei.hianalytics.g.b.d("SendManagerImpl", "send file ,file length full！");
            return null;
        }
        String a = a(file, "MD5");
        String a2 = a(file, AaidIdConstant.SIGNATURE_SHA256);
        String valueOf = String.valueOf(file.length());
        a aVar = new a(a, valueOf, a2);
        List arrayList = new ArrayList();
        arrayList.add(aVar);
        bVar.a("0");
        bVar.b(file.getName());
        bVar.a(arrayList);
        bVar.c(valueOf);
        bVar.d(str2);
        bVar.e(valueOf);
        bVar.f("1");
        bVar.g("0");
        bVar.h("others");
        return bVar;
    }

    public String a(File file, String str) {
        Closeable fileInputStream;
        Throwable th;
        String str2 = "";
        Closeable closeable = null;
        try {
            fileInputStream = new FileInputStream(file);
            try {
                ByteBuffer map = fileInputStream.getChannel().map(MapMode.READ_ONLY, 0, file.length());
                MessageDigest instance = MessageDigest.getInstance(str);
                instance.update(map);
                String bigInteger = new BigInteger(1, instance.digest()).toString(16);
                d.a(1, fileInputStream);
                return bigInteger;
            } catch (IOException e) {
                closeable = fileInputStream;
                try {
                    com.huawei.hianalytics.g.b.d("SendManagerImpl", "getMd5ByFile : file input stream Exception!");
                    d.a(1, closeable);
                    return str2;
                } catch (Throwable th2) {
                    fileInputStream = closeable;
                    th = th2;
                    d.a(1, fileInputStream);
                    throw th;
                }
            } catch (NoSuchAlgorithmException e2) {
                try {
                    com.huawei.hianalytics.g.b.d("SendManagerImpl", "getMd5ByFile : No such algorithm!");
                    d.a(1, fileInputStream);
                    return str2;
                } catch (Throwable th3) {
                    th = th3;
                    d.a(1, fileInputStream);
                    throw th;
                }
            }
        } catch (IOException e3) {
            com.huawei.hianalytics.g.b.d("SendManagerImpl", "getMd5ByFile : file input stream Exception!");
            d.a(1, closeable);
            return str2;
        } catch (NoSuchAlgorithmException e4) {
            fileInputStream = null;
            com.huawei.hianalytics.g.b.d("SendManagerImpl", "getMd5ByFile : No such algorithm!");
            d.a(1, fileInputStream);
            return str2;
        } catch (Throwable th22) {
            fileInputStream = null;
            th = th22;
            d.a(1, fileInputStream);
            throw th;
        }
    }

    public String a(String str, b bVar) {
        if (bVar == null || TextUtils.isEmpty(str)) {
            com.huawei.hianalytics.g.b.d("SendManagerImpl", "commonbody is empty or fileinfo is null!");
            return "";
        }
        List<a> c = bVar.c();
        JSONArray jSONArray = new JSONArray();
        for (a aVar : c) {
            JSONObject jSONObject = new JSONObject();
            jSONObject.put("fileMd5", aVar.c());
            jSONObject.put("fileSha256", aVar.b());
            jSONObject.put("fileSize", aVar.a());
            jSONArray.put(jSONObject);
        }
        return String.format(Locale.getDefault(), "%s=%s&%s=%s&%s=%s&%s=%s&%s=%s&%s=%s&%s=%s&%s=%s&%s=%s", new Object[]{str + ContainerUtils.FIELD_DELIMITER + "logType", bVar.a(), "fileName", bVar.b(), "fileHashList", jSONArray, "fileSize", bVar.d(), "encryptKey", bVar.e(), "patchSize", bVar.f(), "patchNum", bVar.g(), "patchVer", bVar.h(), "others", bVar.i()});
    }

    public List<c> a(JSONArray jSONArray, String str) {
        List<c> arrayList = new ArrayList();
        for (int i = 0; i < jSONArray.length(); i++) {
            JSONObject jSONObject = jSONArray.getJSONObject(i);
            String optString = jSONObject.optString("method");
            String optString2 = jSONObject.optString("uploadUrl");
            jSONObject = jSONObject.optJSONObject("headers");
            c cVar = new c(optString2, optString, str);
            cVar.a("Authorization", jSONObject.optString("Authorization"));
            cVar.a("x-amz-content-sha256", jSONObject.optString("x-amz-content-sha256"));
            cVar.a("x-amz-date", jSONObject.optString("x-amz-date"));
            cVar.a("connection", jSONObject.optString("connection"));
            cVar.a("Host", jSONObject.optString("Host"));
            cVar.a("Content-MD5", jSONObject.optString("Content-MD5"));
            cVar.a(HttpContants.KEY_CONTENT_TYPE, jSONObject.optString(HttpContants.KEY_CONTENT_TYPE));
            cVar.a("user-agent", jSONObject.optString("user-agent"));
            arrayList.add(cVar);
        }
        return arrayList;
    }
}
